#ifdef WITH_CG

#include "shader_catalog_cg.h"
#include "shader_cg.h"

#include <Cg/cg.h>

using namespace krryn::system_imp;
using namespace krryn::view;

namespace{
	void resolve_includes_callback(CGcontext context, const char *filename){
		if(strcmp(filename, "stdlib.cg") == 0){
			cgSetCompilerIncludeFile(context, "stdlib.cg", "stdlib.cg");
		}
	}

	void error_callback(CGcontext context, CGerror error, void* appdata){
		shader_catalog_cg *l_Catalog = (shader_catalog_cg*)appdata;

		const char* l_String = cgGetLastErrorString(&error);
		if (error == CG_COMPILER_ERROR){
			compile_error_args l_Args;
			l_Args.m_ErrorMessage = cgGetLastListing(context);
			l_Catalog->m_OnCompileError.trigger(&l_Args);
		}
	}
}

shader_catalog_cg::shader_catalog_cg(krryn::view::renderer &a_Renderer) : shader_catalog(a_Renderer) {
	m_Context = cgCreateContext();
	cgSetCompilerIncludeCallback(m_Context, &resolve_includes_callback);
	cgSetErrorHandler(&error_callback, this);
}

shader_catalog_cg::~shader_catalog_cg(){
	cgDestroyContext(m_Context);
}

#endif
